/**
 * pharma-ws - 19 mars 2011 guillaume
 */
package org.bspv.pharma.ws.endpoint.management.order;

import org.bspv.pharma.process.ManagementProcessService;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;

/**
 * @author guillaume
 * 
 */
@Endpoint
public class OrderCancellationEndpoint {

    /**
     * Namespace.
     */
    private static final String NAMESPACE_URI = "http://bspv-parma.ws/or/schemas";

    /**
     * Expressions XPath pour l'argument orderId.
     */
    private XPath orderIdExpression;
    /**
     * Expression XPath du commentaire de la commande.
     */
    private XPath orderCommentExpression;

    /**
     * Process service.
     */
    private ManagementProcessService managementProcessService;

    /**
     * Constructeur.
     */
    @Autowired
    public OrderCancellationEndpoint(ManagementProcessService managementProcessService) throws JDOMException {
        this.managementProcessService = managementProcessService;
        // Construction du namespace
        Namespace namespace = Namespace.getNamespace("or", NAMESPACE_URI);
        // Construction des expressions XPath
        orderIdExpression = XPath.newInstance("//or:orderId");
        orderIdExpression.addNamespace(namespace);

        orderCommentExpression = XPath.newInstance("//or:orderComment");
        orderCommentExpression.addNamespace(namespace);
    }

    /**
     * Traitement de la requete.
     */
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "OrderCancellationEndpoint")
    public void handleOrderCancellationEndpoint(@RequestPayload Element request) throws Exception {
        // Récupération des éléments de la requête
        Long orderId = Long.decode(orderIdExpression.valueOf(request));
        String orderComment = orderCommentExpression.valueOf(request);
        Long userId = null;// TODO Récupérer l'identifiant de l'utilisateur
        // Appel du service processus
        managementProcessService.cancelOrderById(orderId, orderComment, userId);
    }
    // @ResponsePayload
    // public Object handle...

}
